import {
  getProvinces,
  bankList,
  bankBranchInfo,
  addProductBank,
  bankcardList,
  upBannk
} from "../../../api.js"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    province_lis: [],
    province_index: -1,
    city_lis: [],
    city_index: -1,
    bank_lis: [],
    bank_index: -1,
    bankBranch_lis: [],
    bankBranch_index: -1,
    form: {
      name: '',
      bankcard_id: '',
      opening_bank: '',
      opening_bank_branch_code: '',
      opening_bank_branch: '',
      opening_bank_code: '',
      provinceCode: '',
      cityCode: ''
    },
    textnum_text: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    if (options.id) {
      bankcardList({
        token: app.globalData.token,
        id: options.id
      }).then(res => {
        console.log(res)
        if (res.data.code == 666) {
          var list = res.data.data.list[0]
          that.setData({
            form: list,
            textnum_text: list.opening_bank + '股份有限公司'
          })
          getProvinces({
            token: app.globalData.token
          }).then(res => {
            for (var i = 0; i < res.data.data.length; i++) {
              if (res.data.data[i].label == list.province_title) {
                that.city_child(res.data.data[i].children, 2, list.city_title)
                that.setData({
                  province_index: i
                })
              }
            }
            that.setData({
              province_lis: res.data.data
            })
          })
          bankList({
            token: app.globalData.token
          }).then(res => {
            for (var l = 0; l < res.data.data.length; l++) {
              if (res.data.data[l].name == list.opening_bank) {
                that.setData({
                  bank_index: l
                })
              }
            }
            that.setData({
              bank_lis: res.data.data
            })
          })
          that.bankBranch(list.opening_bank_code, list.provinceCode, list.cityCode, 1, list.opening_bank_branch_code)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      })
    } else {
      getProvinces({
        token: app.globalData.token
      }).then(res => {
        that.setData({
          province_lis: res.data.data
        })
      })
      bankList({
        token: app.globalData.token
      }).then(res => {
        that.setData({
          bank_lis: res.data.data
        })
      })
    }
  },
  picker_province(e) {
    var num = e.detail.value
    var that = this
    that.city_child(that.data.province_lis[num].children, 1)
    that.setData({
      province_index: num,
      'form.provinceCode': that.data.province_lis[num].value,
      'form.cityCode': '',
      city_index: -1,
      bankBranch_index: -1,
      'form.opening_bank_branch': '',
      'form.opening_bank_branch_code': '',
    })
  },
  city_child(val, type, name) {
    var that = this
    if (type == 1) {
      that.setData({
        city_lis: val
      })
    }
    if (type == 2) {
      for (var i = 0; i < val.length; i++) {
        if (val[i].label == name) {
          that.setData({
            city_lis: val,
            city_index: i
          })
        }
      }
    }
  },
  city_province(e) {
    var num = e.detail.value
    var that = this
    that.setData({
      city_index: num,
      'form.cityCode': that.data.city_lis[num].value,
      bankBranch_index: -1,
      'form.opening_bank_branch': '',
      'form.opening_bank_branch_code': '',
    })
    if (that.data.form.opening_bank_code) {
      var headBankCode = that.data.form.opening_bank_code
      var provinceCode = that.data.form.provinceCode
      var cityCode = that.data.city_lis[num].value
      that.bankBranch(headBankCode, provinceCode, cityCode)
    }
  },
  bank_province(e) {
    var num = e.detail.value
    var that = this
    var textnum = that.data.bank_lis[num].name + '股份有限公司'
    that.setData({
      bank_index: num,
      'form.opening_bank': that.data.bank_lis[num].name,
      'form.opening_bank_code': that.data.bank_lis[num].code,
      textnum_text: textnum,
      bankBranch_index: -1,
      'form.opening_bank_branch': '',
      'form.opening_bank_branch_code': '',
    })
    if (that.data.form.cityCode) {
      var headBankCode = that.data.bank_lis[num].code
      var provinceCode = that.data.form.provinceCode
      var cityCode = that.data.form.cityCode
      that.bankBranch(headBankCode, provinceCode, cityCode)
    }
  },
  bankBranch_province(e) {
    var num = e.detail.value
    var that = this
    that.setData({
      bankBranch_index: num,
      'form.opening_bank_branch': that.data.bankBranch_lis[num].name,
      'form.opening_bank_branch_code': that.data.bankBranch_lis[num].id,
    })
  },
  //分行
  bankBranch(headBank, province, city, types, nums) {
    wx.showLoading({
      title: '正在查询分行',
    })
    var that = this
    bankBranchInfo({
      token: app.globalData.token,
      headBankCode: headBank,
      provinceCode: province,
      cityCode: city
    }).then(res => {
      if (res.data.data == '') {
        wx.hideLoading()
        that.setData({
          bankBranch_lis: [{
            id: '',
            name: '暂无数据'
          }],
          bankBranch_index: 0
        })
      } else {
        var num = -1
        for (var i = 0; i < res.data.data.length; i++) {
          if (types == 1 && res.data.data[i].id == nums) {
            num = i
            console.log('shuju' + i)
          }
          res.data.data[i].name = res.data.data[i].name.replace(that.data.textnum_text, '')
        }
        wx.hideLoading()
        that.setData({
          bankBranch_lis: res.data.data,
          bankBranch_index: num
        })
      }
    })
  },
  formSubmit(e) {
    var formm = e.detail.value
    var that = this
    if (!formm.name) {
      wx.showToast({
        title: '请填写姓名',
        icon: "none"
      })
      return
    }
    if (!formm.card) {
      wx.showToast({
        title: '请填写卡号',
        icon: "none"
      })
      return
    }
    if (!that.data.form.cityCode) {
      wx.showToast({
        title: '请选择省/市',
        icon: "none"
      })
      return
    }
    if (!that.data.form.opening_bank_code) {
      wx.showToast({
        title: '请选择总行',
        icon: "none"
      })
      return
    }
    if (!that.data.form.opening_bank_branch_code) {
      wx.showToast({
        title: '请选择分行',
        icon: "none"
      })
      return
    }
    that.data.form.name = formm.name
    that.data.form.bankcard_id = formm.card
    that.data.form.token = app.globalData.token
    if (that.data.form.bank_id) {
      upBannk(
        that.data.form
      ).then(res => {
        if (res.data.code == 666) {
          wx.navigateBack({
            delta: 1,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      })
    } else {
      addProductBank(
        that.data.form
      ).then(res => {
        if (res.data.code == 666) {
          wx.navigateBack({
            delta: 1,
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      })
    }
  },
})